package de.xt.logging.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import de.xt.logging.gui.Popup;

public class CopyTask {
	public static void copyLog(final File file, final boolean backup, final String backupPath, final String formating, final String pathToStorage, final String prefix, final boolean ooc, final boolean farbcode, final boolean loginNachrichten, final boolean party, final boolean pns, final boolean shouts) {
		try {
			FileInputStream inFile = new FileInputStream(file);
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					inFile));
			String newContent = "";
			StringBuilder sb = new StringBuilder();
			if (reader.ready()) {
				String line = reader.readLine();
				while (line != null) {
					sb.append(line);
					sb.append("\n");
					newContent = sb.toString();
					line = reader.readLine();
				}
			}
			inFile.close();
			if (backup && backupPath != null) {
				String[] pieces = file.getName().split(File.pathSeparator);
				String orgFileName = pieces[pieces.length - 1];
				sb.delete(0, sb.length()-1);
				sb.append(backupPath);
				sb.append("/");
				sb.append(orgFileName);
				FileWriter fwrite = new FileWriter(sb.toString());
				BufferedWriter out = new BufferedWriter(fwrite);
				out.write(newContent);
				out.close();
			}

			String[] patternsForOoc = new String[3];
			patternsForOoc[0] = "\\(\\(.*\\)\\)";
			patternsForOoc[1] = "\\/\\/";
			patternsForOoc[2] = "\\*\\*.*\\*\\*";
			Filter filter = new Filter();
			if (ooc) {
				try {
					newContent = filter.filterReplace(newContent,
							patternsForOoc);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			String[] patternsForCode = new String[6];
			patternsForCode[0] = "<color=[a-zA-Z0-9#]*>";
			patternsForCode[1] = "</color>";
			patternsForCode[2] = "<i>";
			patternsForCode[3] = "</c>";
			patternsForCode[4] = "</i>";
			patternsForCode[5] = "<c=[a-zA-Z0-9#]*>";
			if (farbcode) {
				try {
					newContent = filter.filterReplace(newContent,
							patternsForCode);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			newContent = newContent.replaceAll("  ", " ");

			String[] lines = newContent.split("\n");
			sb.delete(0, sb.length()-1);
			for (String line : lines) {
				boolean toFiltered = false;

				if (!filter.checkIfLineShouldBeFiltered(line, "\\[[\\w]\\]*") && loginNachrichten) {
					toFiltered = true;
				}
				
				if (filter.checkIfLineShouldBeFiltered(line, "^\\[.*\\] .*:  \\[Gruppe\\]|\\[Party\\]")
						&& party) {
					toFiltered = true;
				}

				if (filter.checkIfLineShouldBeFiltered(line, "^\\[.*\\] .*:  \\[Sprechen\\]|\\[Tell\\]")
						&& pns) {
					toFiltered = true;
				}

				if (filter.checkIfLineShouldBeFiltered(line, "^\\[.*\\] .*:  \\[Ruf\\]|\\[Shout\\]")
						&& shouts) {
					toFiltered = true;
				}

				if (!toFiltered) {
					sb.append(line+"\n");
				}
			}

			Date dt = new Date();
			SimpleDateFormat df = new SimpleDateFormat(formating);
			String filename = pathToStorage + "/" + prefix
					+ df.format(dt) + ".log";
			FileWriter fwrite = new FileWriter(filename);

			BufferedWriter out = new BufferedWriter(fwrite,8192);
			out.write(sb.toString());
//			for (String line : content) {
//				out.write(line);
//				out.newLine();
//				out.flush();
//			}
			new Popup("Erfolg!","Log wurde kopiert!");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
